package com.hhit.bigscreenbackmanage.service.impl.zlgl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hhit.bigscreenbackmanage.dao.zlgl.TZlglActualMeasurementMapper;
import com.hhit.bigscreenbackmanage.entity.zlgl.TZlglActualMeasurement;
import com.hhit.bigscreenbackmanage.service.impl.jsgl.TJsqlTysjBdzServiceImpl;
import com.hhit.bigscreenbackmanage.service.zlgl.TZlglActualMeasurementService;
import com.hhit.bigscreenbackmanage.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TZlglActualMeasurementServiceImpl implements TZlglActualMeasurementService {

    private Logger log = LoggerFactory.getLogger(TJsqlTysjBdzServiceImpl.class);

    @Autowired
    private TZlglActualMeasurementMapper tZlglActualMeasurementMapper;

    @Override
    public PageInfo<TZlglActualMeasurement> getTZlglActualMeasurementAll(JSONObject jSONObject) throws Exception{
        if (jSONObject.get("dataTime") == null || "".equals(jSONObject.get("dataTime"))){
            new Exception("需要数据的日期为空！");
        }
        String propName = jSONObject.getString("propName");

        int pageNum = !jSONObject.containsKey("pageNum") ? 1 : Integer.parseInt(jSONObject.get("pageNum").toString());
        int pageSize = !jSONObject.containsKey("pageSize") ? 15 : Integer.parseInt(jSONObject.get("pageSize").toString());
        PageHelper.startPage(pageNum,pageSize);

        List<TZlglActualMeasurement> list = null;
        if ("scslColumnName".equals(propName)){
            list = tZlglActualMeasurementMapper.getTZlglActualMeasurementAll(jSONObject);
        }
        if ("scslsbColumnName".equals(propName)){
            list = tZlglActualMeasurementMapper.getTZlglActualMeasurementEquipmentAll(jSONObject);
        }
        if ("scslzjColumnName".equals(propName)){
            list = tZlglActualMeasurementMapper.getTZlglActualMeasurementPileFoundationAll(jSONObject);
        }
        if ("scslZbmx".equals(propName)){
            list = tZlglActualMeasurementMapper.getZbmx(jSONObject);
        }
        PageInfo<TZlglActualMeasurement> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertTZlglActualMeasurement(MultipartFile[] file, String dataTime, String propName) throws Exception {
        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Map<String, Object>> result = Utils.getExcelData(propName, file);
            if (result.size() > 0) {
                //将Map转为Bean
                List<TZlglActualMeasurement> list = (List<TZlglActualMeasurement>) Utils.mapToEntiry(result, TZlglActualMeasurement.class);
                if (list.size() > 0) {
                    //处理UUID 及时间问题
                    for (TZlglActualMeasurement tZlglActualMeasurement : list) {
                        tZlglActualMeasurement.setId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
                        tZlglActualMeasurement.setDataTime(dataTime);
                    }
                    JSONObject paramJson = new JSONObject();
                    paramJson.put("dataTime",dataTime);
                    List<TZlglActualMeasurement> tZlglActualMeasurementList = new ArrayList<>();
                    if ("scslColumnName".equals(propName)){
                        // 查询 dataTime对应的时间的id,并进行删除
                        tZlglActualMeasurementList = tZlglActualMeasurementMapper.getTZlglActualMeasurementAll(paramJson);
                        if (CollectionUtil.isNotEmpty(tZlglActualMeasurementList)){
                            List<String> ids = tZlglActualMeasurementList.stream().map(TZlglActualMeasurement::getId).collect(Collectors.toList());
                            int deleteRes = tZlglActualMeasurementMapper.batchDeleteZlglActualMeasurementByIds(ids);
                            log.info("insertTZlglActualMeasurement.scslColumnName==>deleteRes:{}",deleteRes);
                        }
                        tZlglActualMeasurementMapper.insertTZlglActualMeasurement(list);
                    }
                    if ("scslsbColumnName".equals(propName)){
                        // 查询 dataTime对应的时间的id,并进行删除
                        tZlglActualMeasurementList = tZlglActualMeasurementMapper.getTZlglActualMeasurementEquipmentAll(paramJson);
                        if (CollectionUtil.isNotEmpty(tZlglActualMeasurementList)){
                            List<String> ids = tZlglActualMeasurementList.stream().map(TZlglActualMeasurement::getId).collect(Collectors.toList());
                            int deleteRes = tZlglActualMeasurementMapper.batchDeleteZlglActualMeasurementEquipmentByIds(ids);
                            log.info("insertTZlglActualMeasurement.scslsbColumnName==>deleteRes:{}",deleteRes);
                        }
                        tZlglActualMeasurementMapper.insertTZlglActualMeasurementEquipment(list);
                    }
                    if ("scslzjColumnName".equals(propName)){
                        tZlglActualMeasurementList = tZlglActualMeasurementMapper.getTZlglActualMeasurementPileFoundationAll(paramJson);
                        if (CollectionUtil.isNotEmpty(tZlglActualMeasurementList)){
                            List<String> ids = tZlglActualMeasurementList.stream().map(TZlglActualMeasurement::getId).collect(Collectors.toList());
                            int deleteRes = tZlglActualMeasurementMapper.batchDeleteZlglActualMeasurementPileFoundationByIds(ids);
                            log.info("insertTZlglActualMeasurement.scslzjColumnName==>deleteRes:{}",deleteRes);
                        }
                        tZlglActualMeasurementMapper.insertTZlglActualMeasurementPileFoundation(list);
                    }
                }
            }
            resultMap.put("flag",true);
        }catch (Exception e){
            resultMap.put("flag",false);
            log.error(e.getMessage());
        }
        return resultMap;
    }

    @Override
    public List<Map<String,Object>> getScslTj(JSONObject json) throws Exception {
        if (json.get("dataTime") == null || "".equals(json.get("dataTime"))){
            new Exception("需要数据的日期为空！");
        }
        return tZlglActualMeasurementMapper.getScslTj(json);
    }
}
